package com.example.demo.controller;

import com.example.demo.service.ObjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.example.demo.modal.Object;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/object")
public class ObjectController {
    @Autowired
    ObjectService objectService;

    @GetMapping
    public String Table_Object(){
        return "object";
    }

    @PostMapping("/data")
    @ResponseBody
    public List<Object> Object_data(){
        return objectService.findAll();
    }

    @PostMapping("/max")
    @ResponseBody
    public int maxId(){
        return objectService.selectMaxId();
    }

    @PostMapping("/save")
    @ResponseBody
    @RequiresPermissions("add")
    public String save(@RequestBody Object object){
        objectService.save(object);
        return "添加成功！";
    }

    @PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("id") int id){
        objectService.delByObj(id);
        return "删除成功！";
    }

    @PostMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody Object object){
        objectService.update(object);
        return "ok";
    }

    @PostMapping("/find/id")
    @ResponseBody
    public List<Object> find_id(@RequestParam("id") int id){
        return objectService.findById(id);
    }

    @PostMapping("/find/name")
    @ResponseBody
    public List<Object> find_name(@RequestParam("name") String name){
        return objectService.findByName(name);
    }

    @PostMapping("/find/abilities")
    @ResponseBody
    public List<Object> find_abilities(@RequestParam("abilities") String abilities){
        return objectService.findByAbilities(abilities);
    }

    @PostMapping("/find/relationship")
    @ResponseBody
    public List<Object> find_relationship(@RequestParam("relationship") String relationship){
        return objectService.findByRelationship(relationship);
    }

    @PostMapping("/find/chara")
    @ResponseBody
    public List<Object> find_chara(@RequestParam("chara") String chara){
        return objectService.findByChara(chara);
    }
}
